package com.ncui.wisdomLearn.config;

import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.stp.SaLoginModel;
import cn.dev33.satoken.util.SaFoxUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * Sa-Token 日志配置
 *
 * @author ncui
 * @since 2024/12/28 22:54
 */
@Slf4j
@Component
public class SaLogForSlf4j implements SaTokenListener {
    @Override
    public void doLogin(String loginType, Object loginId, String tokenValue, SaLoginModel loginModel) {
        log.info("账号 {} 登录成功 (loginType={}), 会话凭证 token={}", loginId, loginType, tokenValue);
    }

    @Override
    public void doLogout(String loginType, Object loginId, String tokenValue) {
        log.info("账号 {} 注销登录 (loginType={}), 会话凭证 token={}", loginId, loginType, tokenValue);
    }

    @Override
    public void doKickout(String loginType, Object loginId, String tokenValue) {
        log.info("账号 {} 被踢下线 (loginType={}), 会话凭证 token={}", loginId, loginType, tokenValue);
    }

    @Override
    public void doReplaced(String loginType, Object loginId, String tokenValue) {
        log.info("账号 {} 被顶下线 (loginType={}), 会话凭证 token={}", loginId, loginType, tokenValue);
    }

    @Override
    public void doDisable(String loginType, Object loginId, String service, int level, long disableTime) {
        log.info("账号 {} [{}服务] 被封禁 (loginType={}), 封禁等级={}, 解封时间为 {}", loginId, loginType, service, level, SaFoxUtil.formatAfterDate(disableTime * 1000));
    }

    @Override
    public void doUntieDisable(String loginType, Object loginId, String service) {
        log.info("账号 {} [{}服务] 解封成功 (loginType={})", loginId, service, loginType);
    }

    @Override
    public void doOpenSafe(String loginType, String tokenValue, String service, long safeTime) {
        log.info("token 二级认证成功, 业务标识={}, 有效期={}秒, Token值={}", service, safeTime, tokenValue);
    }

    @Override
    public void doCloseSafe(String loginType, String tokenValue, String service) {
        log.info("token 二级认证关闭, 业务标识={}, Token值={}", service, tokenValue);
    }

    @Override
    public void doCreateSession(String id) {
        log.info("SaSession [{}] 创建成功", id);
    }

    @Override
    public void doLogoutSession(String id) {
        log.info("SaSession [{}] 注销成功", id);
    }

    @Override
    public void doRenewTimeout(String tokenValue, Object loginId, long timeout) {
        log.info("token 续期成功, {} 秒后到期, 帐号={}, token值={} ", timeout, loginId, tokenValue);
    }
}
